﻿using SchoolCommunicationSystem.Models;
using SCS.Business;
using SCS.Common.Models;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;

namespace SchoolCommunicationSystem.Controllers
{
    public class ViewGradesByCLassController : Controller
    {
        private ViewGradesByClassServices _viewGradesByClassServices = new ViewGradesByClassServices();
        
        /// <summary>
        /// Index
        /// </summary>
        /// <returns>View</returns>
        public virtual ActionResult Index()
        {
            ViewGradesByClassModels school = new ViewGradesByClassModels();
            school.SchoolYear = _viewGradesByClassServices.GetAllSchoolYear();
            return View(school);
        }

        /// <summary>
        /// SelectSchoolYear
        /// </summary>
        /// <param name="SchoolYear">SchoolYear</param>
        /// <returns>PartialView</returns>
        [HttpPost]
        public virtual ActionResult SelectSchoolYear(int? SchoolYear)
        {
            var schoolYear = SchoolYear.HasValue
                ? this._viewGradesByClassServices.Find(SchoolYear.Value)
                : null;
            var term = SchoolYear.HasValue
                ? this._viewGradesByClassServices.Find(SchoolYear.Value).Terms
                : null;

            IEnumerable<ClassInSchoolYear> clas = null;
            if (schoolYear != null)
            {
                clas = _viewGradesByClassServices.GetClassListInViewGradesByClass(schoolYear.schoolYearID,User.Identity.Name);
            }
            int? a = SchoolYear;
            ViewGradesByClassModels school = new ViewGradesByClassModels
            {
                SelectedSchoolYearID = SchoolYear,
                SchoolYear = _viewGradesByClassServices.GetAllSchoolYear(),
                Term = term,
                ClassInSchoolYear = clas,
                _schoolYearID = a
            };

            if (Request.IsAjaxRequest())
            {
                return PartialView("_Term", school);
            }
            else
            {
                return View("Index", school);
            }
        }

        /// <summary>
        /// SelectClass
        /// </summary>
        /// <param name="_schoolYearID">schoolYearID</param>
        /// <param name="Term">Term</param>
        /// <param name="ClassInSchoolYear">ClassInSchoolYear</param>
        /// <returns>PartialView</returns>
        [HttpPost]
        public virtual ActionResult SelectClass(int? _schoolYearID, int? Term, int? ClassInSchoolYear)
        {
            var schoolYear = _schoolYearID.HasValue
                ? this._viewGradesByClassServices.Find(_schoolYearID.Value)
                : null;

            var term = _schoolYearID.HasValue
                ? this._viewGradesByClassServices.Find(_schoolYearID.Value).Terms
                : null;
            IEnumerable<ClassInSchoolYear> clas = null;
            if (schoolYear != null)
            {
                clas = _viewGradesByClassServices.GetClassListInViewGradesByClass(schoolYear.schoolYearID, User.Identity.Name);
            }

            var subject = _viewGradesByClassServices.getSubjectList(_schoolYearID, ClassInSchoolYear);
            List<Subject> subject2=new List<Subject>();
            foreach (var sub in subject)
            {
                if (!subject2.Contains(sub.Subject))
                {
                    subject2.Add(sub.Subject);
                }
            }
            var b = subject2.AsEnumerable();
            ViewGradesByClassModels school = new ViewGradesByClassModels
            {
                SelectedSchoolYearID = _schoolYearID,
                SelectedTermID = Term,
                SelectedClassID = ClassInSchoolYear,
                SchoolYear = _viewGradesByClassServices.GetAllSchoolYear(),
                Term = term,
                ClassInSchoolYear = clas,
                Subject = subject,
                Subject2 = b
            };

            if (Request.IsAjaxRequest())
            {
                return PartialView("_Subject", school);
            }
            else
            {
                return View("Index", school);
            }
        }
        
        /// <summary>
        /// 
        /// </summary>
        /// <param name="_schoolYearID">schoolYearID</param>
        /// <param name="Term">Term</param>
        /// <param name="ClassInSchoolYear">ClassInSchoolYear</param>
        /// <param name="Subject">Subject</param>
        /// <returns>PartialView</returns>
        [HttpPost]
        public virtual ActionResult ViewGradesByClass(int? _schoolYearID, int? Term, int? ClassInSchoolYear, int? Subject)
        {
            //SelectedSchoolYearID = (int)ViewData["SchoolYear"];

            IList<Grade> Grades = null;
            if (_schoolYearID == 0 || Term == 0 || ClassInSchoolYear == 0 || Subject == 0)
            {
                Grades = _viewGradesByClassServices.ViewGradesByClass();
            }
            else
            {
                Grades = _viewGradesByClassServices.ViewGradesByClass(_schoolYearID, Term, ClassInSchoolYear, Subject);
            }
            var data = new ViewGradesByClassModels()
            {
                SchoolYear = _viewGradesByClassServices.GetAllSchoolYear(),
                Grade = Grades
            };
            if (Request.IsAjaxRequest())
            {
                return PartialView("_Result", data);
            }
            else
            {
                return View("Index", data);
            }
        }
    }
}
